package com.xiaoniu.forum.common.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoniu.forum.config.RabbitConfig;
import com.xiaoniu.forum.dao.CommunityTopicDao;
import com.xiaoniu.forum.entity.CommunityTopicEntity;
import com.xiaoniu.forum.service.TopicSearchService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@Component
public class TopicConsumer {

    @Autowired
    private CommunityTopicDao topicDao;
    @Autowired
    private TopicSearchService topicSearchService;


    @RabbitListener(queues = RabbitConfig.UPDATE_TOPIC_QUEUE) //监听更新话题的队列
    public void onUpdateTopicQueueMessage(Message message) {
        //获取到所有更新的话题的信息异步更新到es中
        List<String> ids = JSON.parseArray(new String(message.getBody()), String.class);
        QueryWrapper<CommunityTopicEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids);
        List<CommunityTopicEntity> topics = topicDao.selectList(queryWrapper);
        System.out.println("话题异步更新到es中");
        topicSearchService.bulkUpdateById(topics);
    }
}
